package com.wing.user.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.wing.common.constant.ClientId;
import com.wing.common.exception.BusinessException;
import com.wing.common.utils.JsonResult;
import com.wing.user.service.FaceRecognitionService;
import com.wing.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 人脸识别
 */

@Validated
@Api(value = "人脸识别")
@RestController("faceRecognitionController")
@RequestMapping("/user/admin/face")
@RequiredArgsConstructor
public class FaceRecognitionController {

    private final FaceRecognitionService faceRecognitionService;
    private final UserService userService;

    @ApiOperation(value = "获取企业信息")
    @GetMapping("/getEnterpriseInfo")
    public JsonResult getEnterpriseInfo() {
        return JsonResult.success(faceRecognitionService.getEnterpriseInfo());
    }

    @ApiOperation(value = "通过用户ID获取用户登录token")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, defaultValue = "", example = "", dataType = "Long", paramType = "query")
    })
    @GetMapping("/findByUserId")
    public JsonResult findByUserId(Long userId,Long cabId,String client_id,String client_secret,String version,String enterpriseCode) {
        try {
            if (client_id==null){
                client_id= ClientId.CLIENT_ID_CAB_APP;
            }
            if (client_secret==null){
                client_secret=ClientId.CLIENT_ID_CAB_APP;
            }
            JSONObject json = userService.findByUserId(userId, client_id,client_secret,cabId,version,enterpriseCode);
            return JSONObject.parseObject(json.toString(),JsonResult.class);
        } catch (BusinessException e) {
            return JsonResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "通过用户的ic卡获取用户登录token")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "icCard", value = "ic卡", required = true, defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "cabId", value = "柜子id", required = true, defaultValue = "", example = "", dataType = "long", paramType = "query")
    })
    @GetMapping("/findByUserIcCard")
    public JsonResult findByUserIcCard(String icCard,Long cabId,String clientId,String clientSecret,String version) {
        try {
            JSONObject json = userService.findByUserIcCard(icCard, clientId,clientSecret,cabId,version);
            return JSONObject.parseObject(json.toString(),JsonResult.class);
        } catch (BusinessException e) {
            return JsonResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return JsonResult.fail(e.getMessage());
        }
    }


    @ApiOperation(value = "创建人员库")
    @GetMapping("/createGroup")
    public JsonResult createGroup(String groupId, String groupName) {
        JsonResult jsonResult=faceRecognitionService.createGroup(groupId, groupName);
        return jsonResult;
    }

}
